package org.tigris.atlas.xml.schema.mda.generator;

import org.apache.velocity.VelocityContext;
import org.tigris.atlas.mda.generator.GenerationContext;
import org.tigris.atlas.mda.metadata.MetadataRepository;
import org.tigris.atlas.mda.metadata.element.Entity;
import org.tigris.atlas.mda.metadata.element.Operation;
import org.tigris.atlas.mda.metadata.element.Service;
import org.tigris.atlas.xml.schema.mda.element.SchemaEntity;

public class SchemaEntityServiceResponseGenerator extends AbstractSchemaServiceResponseGenerator {
	
	/**
	 * @see org.tigris.atlas.mda.codegen.generator.schema.AbstractSchemaServiceResponseGenerator#shouldGenerate(org.tigris.atlas.mda.codegen.element.java.JavaOperation)
	 */
	protected boolean shouldGenerate(Operation operation) {
		return  isSingleEntityResponse(operation);
	}
	
	/**
	 * @see org.tigris.atlas.mda.codegen.generator.service.AbstractServiceResponseJavaGenerator#setupContext(org.tigris.atlas.mda.codegen.generator.GenerationContext, org.tigris.atlas.mda.codegen.element.java.JavaService, org.tigris.atlas.mda.codegen.element.java.JavaOperation)
	 */
	protected VelocityContext setupContext(GenerationContext context, Service service, Operation operation) {	
		String applicationName = context.getArtifactId();
		VelocityContext vc = super.setupContext(context, service, operation);
		 
		//this template is also used to generate responses for entities from the 
		//entity maintenance server, so the properties are a little more abstract:
		vc.put("responseName", operation.getName());
		
		String entityName = operation.getReturnType();
		Entity entity = (Entity)MetadataRepository.getInstance().getAllEntities(applicationName).get(entityName);
		SchemaEntity schemaEntity = new SchemaEntity(entity);
		vc.put("entity", schemaEntity);
		vc.put("serviceResponseQualifier", "");
		 
		return vc;
	}	

}
